package com.fbooks.service.Impl;


import com.alibaba.fastjson.JSONObject;
import com.fbooks.bean.*;
import com.fbooks.mapper.*;
import com.fbooks.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: LuoRunFeng
 * @Email: 475403147@qq.com
 * @Time: 2020/5/23 0023 10:38
 * @Team: 蜂鸟工作室
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private ArticleCatalogMapper articleCatalogMapper;
    @Resource
    private ArticleLabelsMapper articleLabelsMapper;
    @Resource
    private ArticleLikeMapper articleLikeMapper;
    @Resource
    private ArticleCommentMapper articleCommentMapper;
    @Resource
    private ArticleCollectMapper articleCollectMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 发布
     * @param article
     * @return
     */
    public JSONObject releaseArticle(Article article) {
        JSONObject backInfo =new JSONObject();
        //初始化数据
        article.setViewsCount("0");
        article.setCreateTime(new Timestamp(System.currentTimeMillis()));
        System.out.println(article);
        try {
            //发布文章  返回id
            articleMapper.insert(article);
            //插入labels
            for (ArticleLabels label : article.getLabels()) {
                label.setArticleId(article.getId());
                articleLabelsMapper.insertSelective(label);
            }
            backInfo.put("info", "发布成功！");
            backInfo.put("isSuccess", true);
        }catch (RuntimeException e){
            backInfo.put("info", "发布失败！");
            backInfo.put("isSuccess", false);
            e.printStackTrace();
        }
        return backInfo;
    }


    /**
     * articleid 获取文章
     * @param articleId
     * @return
     */
    public JSONObject selectArticleById(String articleId) {
        JSONObject jsonObject = new JSONObject();
        //这里需要增加一条游览量
        articleMapper.updateViewCount(articleId);
        //将文章类容返回
        Article article = articleMapper.selectByPrimaryKey(articleId);
        jsonObject.put("article",article);

        //获取到分类
        jsonObject.put("catalog",articleCatalogMapper.selectByPrimaryKey(article.getArticleCatalogId()));

        //获取文章标签
        Example example = new Example(ArticleLabels.class);
        example.createCriteria().andEqualTo("articleId",articleId);
        jsonObject.put("labels",articleLabelsMapper.selectByExample(example));

        //获取文章点赞
        Example example1 = new Example(ArticleLike.class);
        example.createCriteria().andEqualTo("articleId",articleId);
        jsonObject.put("likes",articleLikeMapper.selectByExample(example));
        //获取文章收藏
        Example example2 = new Example(ArticleCollect.class);
        example.createCriteria().andEqualTo("articleId",articleId);
        jsonObject.put("collects",articleCollectMapper.selectByExample(example));


        //获取文章评论
        Example example3 = new Example(ArticleComment.class);
        example.createCriteria().andEqualTo("articleId",articleId);
        List<ArticleComment> articleComments = articleCommentMapper.selectByExample(example);
        //还要获取评论的用户信息
        for (ArticleComment articleComment : articleComments) {
            String commentUserId = articleComment.getUserId();
            articleComment.setUser(userMapper.getUserInfoById(commentUserId));
        }
        jsonObject.put("commonts",articleComments);

        return jsonObject;
    }


    /**
     * 用户名下的文章数
     * @return
     */
    public List<Article> getAllArticle(String uid) {
        Example example = new Example(Article.class);
        example.createCriteria().andEqualTo("userId",uid);
        List<Article> articles = articleMapper.selectByExample(example);

        //便利文章
        for (Article article : articles) {
            String articleId =article.getId();
            //点赞数
            ArticleLike articleLike = new ArticleLike();
            articleLike.setArticleId(articleId);
            String likenum =String.valueOf(articleLikeMapper.selectCount(articleLike));
            article.setLikeNum(likenum);

            //收藏数
            ArticleCollect articleCollect = new ArticleCollect();
            articleCollect.setArticleId(articleId);
            String collectnum = String.valueOf(articleCollectMapper.selectCount(articleCollect));
            article.setCollectNum(collectnum);

            //评论数
            ArticleComment articleComment = new ArticleComment();
            articleComment.setArticleId(articleId);
            String commentnum = String.valueOf(articleCommentMapper.selectCount(articleComment));
            article.setCommentNum(commentnum);
        }
        return articles;
    }



    /**
     * 通过id删除文章
    * @param id
     * @return
     */
    public JSONObject deleteArticleById(String id) {
        JSONObject backInfo =new JSONObject();
        int i = articleMapper.deleteByPrimaryKey(id);
        if(i == 1){
            backInfo.put("isSuccess",true);
            backInfo.put("info","删除成功");
            return backInfo;
        }
        backInfo.put("isSuccess",false);
        backInfo.put("info","删除失败");
        return backInfo;
    }


    /**
     * 获取文章分类
     * @return
     */
    public List<ArticleCatalog> getArticleCatalog() {
        List<ArticleCatalog> articleCatalogList = articleCatalogMapper.selectAll();
        return articleCatalogList;
    }

    /**
     * 文章分页查询
     * @param page
     * @param pageSize
     * @return
     */
    public PageInfo<Article> getArticleByPages(String page, String pageSize) {
        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(pageSize));
        List<Article> articles = articleMapper.selectAll();

        //便利文章
        for (Article article : articles) {
            String articleId =article.getId();
            //点赞数
            ArticleLike articleLike = new ArticleLike();
            articleLike.setArticleId(articleId);
            String likenum =String.valueOf(articleLikeMapper.selectCount(articleLike));
            article.setLikeNum(likenum);

            //收藏数
            ArticleCollect articleCollect = new ArticleCollect();
            articleCollect.setArticleId(articleId);
            String collectnum = String.valueOf(articleCollectMapper.selectCount(articleCollect));
            article.setCollectNum(collectnum);

            //评论数
            ArticleComment articleComment = new ArticleComment();
            articleComment.setArticleId(articleId);
            String commentnum = String.valueOf(articleCommentMapper.selectCount(articleComment));
            article.setCommentNum(commentnum);

            //分类
            String articleCatalog = articleCatalogMapper.selectByPrimaryKey(article.getArticleCatalogId()).getArticleCatalog();
            article.setCatalog(articleCatalog);

            //作者信息
            article.setUser(userMapper.getUserInfoById(article.getUserId()));
        }
        PageInfo<Article> pageInfo = new PageInfo<>(articles);

        JSONObject jsonObject = new JSONObject();

        return pageInfo;
    }


    /**
     * 获取用户文章分类（分类下有文章的那种）
     * @param uid
     * @return
     */
    public List<JSONObject> getUserArticleCatalog(String uid) {
//        Map<String,JSONObject> map = new HashMap<String, JSONObject>();
        List<JSONObject> catalogs = new ArrayList<JSONObject>();

        List<JSONObject> articleCatalog = articleMapper.getArticleCatalog(uid);
        for (JSONObject jsonObject : articleCatalog) {
            ArticleCatalog article_catalog = articleCatalogMapper.selectByPrimaryKey(jsonObject.get("article_catalog_id"));
            JSONObject json = new JSONObject();
            json.put("id",article_catalog.getId());
            json.put("catalog",article_catalog.getArticleCatalog());
            catalogs.add(json);
        }
        return catalogs;
    }

    @Override
    public List<Article> getCatalogArticle(String uid, String catalogId) {
        Example example = new Example(Article.class);
        example.createCriteria().andEqualTo("userId",uid).andEqualTo("articleCatalogId",catalogId);
        List<Article> articles = articleMapper.selectByExample(example);
        //便利文章
        for (Article article : articles) {
            String articleId =article.getId();
            //点赞数
            ArticleLike articleLike = new ArticleLike();
            articleLike.setArticleId(articleId);
            String likenum =String.valueOf(articleLikeMapper.selectCount(articleLike));
            article.setLikeNum(likenum);

            //收藏数
            ArticleCollect articleCollect = new ArticleCollect();
            articleCollect.setArticleId(articleId);
            String collectnum = String.valueOf(articleCollectMapper.selectCount(articleCollect));
            article.setCollectNum(collectnum);

            //评论数
            ArticleComment articleComment = new ArticleComment();
            articleComment.setArticleId(articleId);
            String commentnum = String.valueOf(articleCommentMapper.selectCount(articleComment));
            article.setCommentNum(commentnum);
        }
        return articles;
    }
}
